import java.util.*;

public class PaperSelection {
    private final Map<Paper, Student> selections = new HashMap<>();

    public boolean addSelection(Student student, Paper paper) {
        selections.put(paper, student);
        return true;
    }

    public boolean isSelected(Paper paper) {
        return selections.containsKey(paper);
    }

    public Map<Paper, Student> getSelections() {
        return Collections.unmodifiableMap(selections);
    }

    public void accept(PaperSelectionVisitor visitor) {
        visitor.visit(this);
    }

    public Set<Student> getStudentsWithSelections() {
        return new HashSet<>(selections.values());
    }

    public List<Paper> getPapersByType(PaperType type) {
        List<Paper> result = new ArrayList<>();
        for (Paper p : selections.keySet()) {
            if (type == PaperType.Any ||
                    p.getClass().getSimpleName().equals(type.name())) {
                result.add(p);
            }
        }
        return result;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (Paper p : selections.keySet()) {
            sb.append(p.toString())
                    .append("\tselected by ")
                    .append(selections.get(p).getName())
                    .append("\n");
        }
        return sb.toString();
    }
}